package com.genesis.mall.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.genesis.mall.common.PageInfo;
import com.genesis.mall.dto.CouponHistoryDetailDto;
import com.genesis.mall.entity.SmsCoupon;
import com.genesis.mall.entity.SmsCouponProductCategoryRelation;
import com.genesis.mall.entity.SmsCouponProductRelation;
import com.genesis.mall.mapper.SmsCouponMapper;
import com.genesis.mall.service.ISmsCouponProductCategoryRelationService;
import com.genesis.mall.service.ISmsCouponProductRelationService;
import com.genesis.mall.service.ISmsCouponService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import com.genesis.mall.vo.SmsCouponVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 优惠券表 服务实现类
 * </p>
 *
 * @author author
 * @since 2022-08-01
 */
@Service
public class SmsCouponServiceImpl extends ServiceImpl<SmsCouponMapper, SmsCoupon> implements ISmsCouponService {
    @Autowired
    private ISmsCouponProductCategoryRelationService productCategoryRelationService;

    @Autowired
    private ISmsCouponProductRelationService productRelationService;

    /**
     * 添加优惠券
     *
     * @param couponVo
     */
    @Override
    public void createCoupon(SmsCouponVo couponVo) {
        this.save(couponVo);
        productCategoryRelationService.saveBatch(couponVo.getProductCategoryRelationList());
        productRelationService.saveBatch(couponVo.getProductRelationList());
    }

    /**
     * 删除优惠券
     * @param id
     */
    @Override
    public void deleteCoupon(Integer id) {
        this.removeById(id);
        //删除优惠券和产品分类关系表
        LambdaQueryWrapper<SmsCouponProductCategoryRelation> productCategoryRelationQueryWrapper = new LambdaQueryWrapper<>();
        productCategoryRelationQueryWrapper.eq(SmsCouponProductCategoryRelation::getCouponId,id);
        productCategoryRelationService.remove(productCategoryRelationQueryWrapper);

        //删除优惠券和产品的关系表
        LambdaQueryWrapper<SmsCouponProductRelation> productRelationQueryWrapper = new LambdaQueryWrapper<>();
        productRelationQueryWrapper.eq(SmsCouponProductRelation::getCouponId,id);
        productRelationService.remove(productRelationQueryWrapper);
    }

    /**
     * 根据优惠券名称和类型分页获取优惠券列表
     * @param name
     * @param pageNum
     * @param pageSize
     * @param type
     * @return
     */
    @Override
    public PageInfo listCoupon(String name, Integer pageNum, Integer pageSize, Integer type) {
        Page<SmsCoupon> page = new Page<>(pageNum,pageSize);

        LambdaQueryWrapper<SmsCoupon> lqw = new LambdaQueryWrapper<>();
        lqw.like(StringUtils.isNotEmpty(name),SmsCoupon::getName,name)
                .eq(type != null, SmsCoupon::getType,type);

        Page<SmsCoupon> couponPage = this.page(page, lqw);
        List<SmsCoupon> couponList = couponPage.getRecords();
        int total = (int) couponPage.getTotal();
        Integer totalPage = PageInfo.totalPage(pageSize, total);

        //封装返回数据
        PageInfo pageInfo = new PageInfo();
        pageInfo.setPageNum(pageNum);
        pageInfo.setPageSize(pageSize);
        pageInfo.setTotal(total);
        pageInfo.setTotalPage(totalPage);
        pageInfo.setList(couponList);

        return pageInfo;
    }

    /**
     * 修改优惠券
     * @param couponVo
     * @param id
     */
    @Override
    public void updateCoupon(SmsCouponVo couponVo, Integer id) {
        this.updateById(couponVo);
        productCategoryRelationService.updateBatchById(couponVo.getProductCategoryRelationList());
        productRelationService.updateBatchById(couponVo.getProductRelationList());
    }

    /**
     * 获取单个优惠券的详细信息
     * @param id
     * @return
     */
    @Override
    public SmsCouponVo getOneCoupon(Integer id) {
        //获取优惠券表数据
        LambdaQueryWrapper<SmsCoupon> couponQueryWrapper = new LambdaQueryWrapper<>();
        couponQueryWrapper.eq(SmsCoupon::getId,id);
        SmsCoupon oneCoupon = this.getOne(couponQueryWrapper);

        //获取优惠券和产品分类关系表数据
        LambdaQueryWrapper<SmsCouponProductCategoryRelation> couponProductCategoryRelationQueryWrapper = new LambdaQueryWrapper<>();
        couponProductCategoryRelationQueryWrapper.eq(SmsCouponProductCategoryRelation::getCouponId,id);
        List<SmsCouponProductCategoryRelation> couponProductCategoryRelations = productCategoryRelationService.list(couponProductCategoryRelationQueryWrapper);

        //获取优惠券和产品的关系表数据
        LambdaQueryWrapper<SmsCouponProductRelation> couponProductRelationQueryWrapper = new LambdaQueryWrapper<>();
        couponProductRelationQueryWrapper.eq(SmsCouponProductRelation::getCouponId,id);
        List<SmsCouponProductRelation> couponProductRelations = productRelationService.list(couponProductRelationQueryWrapper);

        //封装数据
        SmsCouponVo couponVo = new SmsCouponVo();
        BeanUtils.copyProperties(oneCoupon,couponVo);
        couponVo.setProductCategoryRelationList(couponProductCategoryRelations);
        couponVo.setProductRelationList(couponProductRelations);

        return couponVo;
    }
    @Autowired
    private SmsCouponMapper couponMapper;
    @Override
    public List<SmsCoupon> findByCouponId(Long couponId) {
        return couponMapper.findByCouponId(couponId);
    }
}
